#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

// ====== STRUCTURE ======
typedef struct {
    float ca_total;
    int v_cafe;
    int v_the;
    int v_choc;
    int v_cap;
} Stats;

// ====== PROTOTYPES ======
void menuClient(Stats *s);
void espaceAdmin(Stats *s);
void sauvegarderStats(Stats s);
void chargerStats(Stats *s);
void color(int c);

// ====== COLORS ======
void color(int c) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

// ====== MAIN ======
int main() {

    Stats machine = {0.0, 0, 0, 0, 0};

    chargerStats(&machine);

    menuClient(&machine);

    return 0;
}

// ====== MENU CLIENT ======
void menuClient(Stats *s) {

    int choix;
    float prix, insere, total_insere;

    do {

        system("cls");

        color(11);
        printf("\n====================================================\n");

        color(14);
        printf("         DISTRIBUTEUR AUTOMATIQUE DE BOISSONS\n");

        color(11);
        printf("====================================================\n");

        color(10);
        printf(" 1. Cafe Court ...................... 0.40 EUR\n");

        color(2);
        printf(" 2. The a la Menthe ................. 0.50 EUR\n");

        color(6);
        printf(" 3. Chocolat Chaud .................. 0.60 EUR\n");

        color(13);
        printf(" 4. Cappuccino ...................... 0.80 EUR\n");

        color(12);
        printf(" 999. ESPACE ADMINISTRATION\n");

        color(15);
        printf(" 0. Quitter\n");

        color(11);
        printf("====================================================\n");

        color(15);
        printf("Votre choix : ");

        if(scanf("%d", &choix) != 1) {
            while(getchar() != '\n');
            continue;
        }

        // ====== CHOIX BOISSON ======
        if(choix >= 1 && choix <= 4) {

            if(choix == 1) prix = 0.40f;
            else if(choix == 2) prix = 0.50f;
            else if(choix == 3) prix = 0.60f;
            else prix = 0.80f;

            color(14);
            printf("\nPrix : %.2f EUR\n", prix);

            total_insere = 0;

            // ====== PAIEMENT ======
            while(total_insere < prix - 0.001f) {

                color(15);

                printf("\nInserez une piece (0.10 / 0.20 / 0.50 / 1 / 2) : ");

                if(scanf("%f", &insere) != 1) {
                    while(getchar() != '\n');
                    continue;
                }

                if(
                    (insere > 0.09 && insere < 0.11) ||
                    (insere > 0.19 && insere < 0.21) ||
                    (insere > 0.49 && insere < 0.51) ||
                    (insere > 0.99 && insere < 1.01) ||
                    (insere > 1.99 && insere < 2.01)
                ) {

                    total_insere += insere;

                    color(10);
                    printf("Vous avez insere : %.2f EUR\n",
                           total_insere);

                    if(total_insere < prix) {

                        color(6);
                        printf("Reste a payer : %.2f EUR\n",
                               prix - total_insere);
                    }

                } else {

                    color(12);
                    printf("[!] Piece refusee.\n");
                }
            }

            // ====== ANIMATION ======
            color(14);
            printf("\nPreparation");

            for(int i = 0; i < 3; i++) {
                printf(".");
                Sleep(250);
            }

            printf("\n");

            // ====== MONNAIE ======
            if(total_insere > prix) {

                color(11);
                printf("Rendu de monnaie : %.2f EUR\n",
                       total_insere - prix);
            }

            color(10);
            printf("Votre boisson est prete !\n");

            // ====== STATS ======
            s->ca_total += prix;

            if(choix == 1) s->v_cafe++;
            else if(choix == 2) s->v_the++;
            else if(choix == 3) s->v_choc++;
            else if(choix == 4) s->v_cap++;

            sauvegarderStats(*s);

            color(15);
            printf("\nAppuyez sur une touche...");
            getch();
        }

        // ====== ADMIN ======
        else if(choix == 999) {
            espaceAdmin(s);
        }

    } while(choix != 0);
}

// ====== ESPACE ADMIN ======
void espaceAdmin(Stats *s) {

    char log[20], pas[20], sLog[20], sPas[20];

    FILE *f = fopen("auth.txt", "r");

    system("cls");

    // ====== CREATION COMPTE ======
    if(f == NULL) {

        color(14);
        printf("===== CONFIGURATION ADMIN =====\n");

        color(15);
        printf("Nouvel utilisateur : ");
        scanf("%s", log);

        printf("Nouveau mot de passe : ");
        scanf("%s", pas);

        f = fopen("auth.txt", "w");

        if(f) {

            fprintf(f, "%s %s", log, pas);

            fclose(f);

            color(10);
            printf("Compte cree avec succes !\n");
        }
    }

    else {

        fscanf(f, "%s %s", sLog, sPas);

        fclose(f);

        color(11);
        printf("===== CONNEXION ADMIN =====\n");

        color(15);
        printf("Login : ");
        scanf("%s", log);

        printf("Password : ");
        scanf("%s", pas);

        // ====== AUTH ======
        if(strcmp(log, sLog) == 0 &&
           strcmp(pas, sPas) == 0) {

            int a;

            do {

                system("cls");

                color(13);
                printf("========== MENU ADMIN ==========\n");

                color(10);
                printf("1. Chiffre d'affaires\n");

                color(11);
                printf("2. Nombre des ventes\n");

                color(14);
                printf("3. Modifier mot de passe\n");

                color(12);
                printf("4. Reset statistiques\n");

                color(15);
                printf("0. Retour\n");

                printf("Choix : ");

                scanf("%d", &a);

                // ====== CA ======
                if(a == 1) {

                    color(10);
                    printf("\nCA Total : %.2f EUR\n",
                           s->ca_total);

                    system("pause");
                }

                // ====== VENTES ======
                else if(a == 2) {

                    color(11);

                    printf("\nCafe       : %d\n", s->v_cafe);
                    printf("The        : %d\n", s->v_the);
                    printf("Chocolat   : %d\n", s->v_choc);
                    printf("Cappuccino : %d\n", s->v_cap);

                    system("pause");
                }

                // ====== NEW PASSWORD ======
                else if(a == 3) {

                    color(14);

                    printf("Nouveau mot de passe : ");

                    scanf("%s", pas);

                    FILE *nf = fopen("auth.txt", "w");

                    fprintf(nf, "%s %s", sLog, pas);

                    fclose(nf);

                    color(10);
                    printf("Mot de passe modifie !\n");

                    system("pause");
                }

                // ====== RESET ======
                else if(a == 4) {

                    s->ca_total = 0;
                    s->v_cafe = 0;
                    s->v_the = 0;
                    s->v_choc = 0;
                    s->v_cap = 0;

                    sauvegarderStats(*s);

                    color(12);
                    printf("Statistiques reinitialisees !\n");

                    system("pause");
                }

            } while(a != 0);
        }

        else {

            color(12);
            printf("Erreur d'authentification.\n");

            system("pause");
        }
    }
}

// ====== SAVE ======
void sauvegarderStats(Stats s) {

    FILE *f = fopen("vente.dat", "wb");

    if(f) {

        fwrite(&s, sizeof(Stats), 1, f);

        fclose(f);
    }
}

// ====== LOAD ======
void chargerStats(Stats *s) {

    FILE *f = fopen("vente.dat", "rb");

    if(f) {

        fread(s, sizeof(Stats), 1, f);

        fclose(f);
    }
}

